home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / bytesc88.arc / CC11.C < prev    next >
Text File  |  1988-05-12  |  6KB  |  243 lines

  1.  
  2. /*
  3. ** Small-C, 8088/8086 version -- modified by R. Grehan, BYTE Magazine
  4. ** execution begins here
  5. */
  6. main(argc, argv) int argc, *argv; {
  7.   argcs=argc;
  8.   argvs=argv;
  9.   fputs("Small-C Compiler, ", stderr); fputs(VERSION, stderr);
  10.   fputs(CRIGHT1, stderr); fputs(CRIGHT2,stderr);
  11. #ifdef DYNAMIC
  12.   swnext=calloc(SWTABSZ, 1);
  13.   swend=swnext+((SWTABSZ-SWSIZ)>>1);
  14.   stage=calloc(STAGESIZE, 1);
  15.   stagelast=stage+STAGELIMIT;
  16.   wq=calloc(WQTABSZ, BPW);
  17.   litq=calloc(LITABSZ, 1);
  18.   macn=calloc(MACNSIZE, 1);
  19.   macq=calloc(MACQSIZE, 1);
  20.   pline=calloc(LINESIZE, 1);
  21.   mline=calloc(LINESIZE, 1);
  22. #else
  23.   swend=(swnext=swq)+SWTABSZ-SWSIZ;
  24.   stagelast=stage+STAGELIMIT;
  25. #endif
  26.   swactive=        /* not in switch */
  27.   stagenext=        /* direct output mode */
  28.   iflevel=        /* #if... nesting level = 0 */
  29.   skiplevel=        /* #if... not encountered */
  30.   macptr=        /* clear the macro pool */
  31.   csp=            /* stack ptr (relative) */
  32.   errflag=        /* not skipping errors till ";" */
  33.   eof=            /* not eof yet */
  34.   ncmp=            /* not in compound statement */
  35.   files=
  36.   filearg=
  37.   swused=
  38.   quote[1]=0;
  39.   func1=        /* first function */
  40.   ccode=1;        /* enable preprocessing */
  41.   wqptr=wq;        /* clear while queue */
  42.   quote[0]='"';        /* fake a quote literal */
  43.   input=input2=EOF;
  44.   ask();        /* get user options */
  45.   openfile();        /* and initial input file */
  46.   preprocess();        /* fetch first line */
  47. #ifdef DYNAMIC
  48.   symtab=calloc((NUMLOCS*SYMAVG + NUMGLBS*SYMMAX), 1);
  49. #endif
  50.   locptr=STARTLOC;
  51.   glbptr=STARTGLB;
  52.   glbflag=1;
  53.   ctext=0;
  54.   header();        /* intro code */
  55.   setops();        /* set values in op arrays */
  56.   parse();        /* process ALL input */
  57.   outside();        /* verify outside any function */
  58.   trailer();        /* follow-up code */
  59.   fclose(output);
  60.   }
  61.  
  62. /*
  63. ** process all input text
  64. **
  65. ** At this level, only static declarations,
  66. **      defines, includes and function
  67. **      definitions are legal...
  68. */
  69. parse() {
  70.   while (eof==0) {
  71.     if(amatch("extern", 6))   dodeclare(EXTERNAL);
  72.     else if(dodeclare(STATIC));
  73.     else if(match("#asm"))    doasm();
  74.     else if(match("#include"))doinclude();
  75.     else if(match("#define")) addmac();
  76.     else                      newfunc();
  77.     blanks();        /* force eof if pending */
  78.     }
  79.   }
  80.  
  81. /*
  82. ** dump the literal pool
  83. */
  84. dumplits(size) int size; {
  85.   int j, k;
  86.   k=0;
  87.   while (k<litptr) {
  88.     poll(1);        /* allow program interruption */
  89.     defstorage(size);
  90.     j=10;
  91.     while(j--) {
  92.       outdec(getint(litq+k, size));
  93.       k=k+size;
  94.       if ((j==0)|(k>=litptr)) {nl(); break;}
  95.       outbyte(',');
  96.       }
  97.     }
  98.   }
  99.  
  100. /*
  101. ** dump zeroes for default initial values
  102. */
  103. dumpzero(size, count) int size, count; {
  104.   int j;
  105.   while (count > 0) {
  106.     poll(1);        /* allow program interruption */
  107.     defstorage(size);
  108.     j=30;
  109.     while(j--) {
  110.       outdec(0);
  111.       if ((--count <= 0)|(j==0)) {nl(); break;}
  112.       outbyte(',');
  113.       }
  114.     }
  115.   }
  116.  
  117. /*
  118. ** verify compile ends outside any function
  119. */
  120. outside()  {
  121.   if (ncmp) error("no closing bracket");
  122.   }
  123.  
  124. /*
  125. ** get run options
  126. */
  127. ask() {
  128.   int i;
  129.   i=listfp=nxtlab=0;
  130.   output=stdout;
  131. #ifdef OPTIMIZE
  132.   optimize=
  133. #endif
  134.   alarm=monitor=pause=NO;
  135.   line=mline;
  136.   while(getarg(++i, line, LINESIZE, argcs, argvs)!=EOF) {
  137.     if(line[0]!='-') continue;
  138.     if((toupper(line[1])=='L')&(isdigit(line[2]))&(line[3]<=' ')) {
  139.       listfp=line[2]-'0';
  140.       continue;
  141.       }
  142.     if(line[2]<=' ') {
  143.       if(toupper(line[1])=='A') {
  144.         alarm=YES;
  145.         continue;
  146.         }
  147.       if(toupper(line[1])=='M') {
  148.         monitor=YES;
  149.         continue;
  150.         }
  151. #ifdef OPTIMIZE
  152.       if(toupper(line[1])=='O') {
  153.         optimize=YES;
  154.         continue;
  155.         }
  156. #endif
  157.       if(toupper(line[1])=='P') {
  158.         pause=YES;
  159.         continue;
  160.         }
  161.       }
  162. #ifndef LINK
  163.     if(toupper(line[1])=='B') {
  164.       bump(0); bump(2);
  165.       if(number(&nxtlab)) continue;
  166.       }
  167. #endif
  168.     sout("usage: cc [file]... [-m] [-a] [-p] [-l#]", stderr);
  169. #ifdef OPTIMIZE
  170.     sout(" [-o]", stderr);
  171. #endif
  172. #ifndef LINK
  173.     sout(" [-b#]", stderr);
  174. #endif
  175.     sout(NEWLINE, stderr);
  176.     abort(ERRCODE);
  177.     }
  178.   }
  179.  
  180. /*
  181. ** input and output file opens
  182. */
  183. openfile() {        /* entire function revised */
  184.   char outfn[15];
  185.   int i, j, ext;
  186.   input=EOF;
  187.   while(getarg(++filearg, pline, LINESIZE, argcs, argvs)!=EOF) {
  188.     if(pline[0]=='-') continue;
  189.     ext = NO;
  190.     i = -1;
  191.     j = 0;
  192.     while(pline[++i]) {
  193.       if(pline[i] == '.') {ext = YES; break;}
  194.       if(j < 10) outfn[j++] = pline[i];
  195.       }
  196.     if(!ext) {
  197.       strcpy(pline + i, ".C");
  198.       }
  199.     input = mustopen(pline, "r");
  200.     if(!files && isatty(stdout)) {
  201.       strcpy(outfn + j, ".MAC");
  202.       output = mustopen(outfn, "w");
  203.       }
  204.     files=YES;
  205.     kill();
  206.     return;
  207.     }
  208.   if(files++) eof=YES;
  209.   else input=stdin;
  210.   kill();
  211.   }
  212.  
  213. /*
  214. ** open a file with error checking
  215. */
  216. mustopen(fn, mode) char *fn, *mode; {
  217.   int fd;
  218.   if(fd = fopen(fn, mode)) return fd;
  219.   sout("open error on ", stderr);
  220.   lout(fn, stderr);
  221.   abort(ERRCODE);
  222.   }
  223.  
  224. setops() {
  225.   op2[ 0]=     op[ 0]=  ffor;        /* heir5 */
  226.   op2[ 1]=     op[ 1]= ffxor;        /* heir6 */
  227.   op2[ 2]=     op[ 2]= ffand;        /* heir7 */
  228.   op2[ 3]=     op[ 3]=  ffeq;        /* heir8 */
  229.   op2[ 4]=     op[ 4]=  ffne;
  230.   op2[ 5]=ule; op[ 5]=  ffle;        /* heir9 */
  231.   op2[ 6]=uge; op[ 6]=  ffge;
  232.   op2[ 7]=ult; op[ 7]=  fflt;
  233.   op2[ 8]=ugt; op[ 8]=  ffgt;
  234.   op2[ 9]=     op[ 9]= ffasr;        /* heir10 */
  235.   op2[10]=     op[10]= ffasl;
  236.   op2[11]=     op[11]= ffadd;        /* heir11 */
  237.   op2[12]=     op[12]= ffsub;
  238.   op2[13]=     op[13]=ffmult;        /* heir12 */
  239.   op2[14]=     op[14]= ffdiv;
  240.   op2[15]=     op[15]= ffmod;
  241.   }
  242.  
  243.